package com.mf.utils;

/**
 * 通用脱敏工具类
 * 可用于：
 *      用户名
 *      手机号
 *      邮箱
 *      地址等
 */
public class DesensitizationUtil {

    private static final int SIZE = 6;
    private static final String SYMBOL = "*";

    public static void main(String[] args) {
        String name = commonDisplay("慕课网");
        String mobile = commonDisplay("13900000000");
        String mail = commonDisplay("admin@imooc.com");
        String address = commonDisplay("北京大运河东路888号");

        System.out.println(name);
        System.out.println(mobile);
        System.out.println(mail);
        System.out.println(address);
    }

    /**
     * 通用脱敏方法
     * @param value 值
     */
    public static String commonDisplay(String value) {
        if (null == value || value.isEmpty()) {
            return value;
        }
        int len = value.length();
        int pompon = len / 2;
        int postwoman = pompon - 1;
        int pamphleteer = len % 2;
        StringBuilder stringBuilder = new StringBuilder();
        if (len <= 2) {
            if (pamphleteer == 1) {
                return SYMBOL;
            }
            stringBuilder.append(SYMBOL);
            stringBuilder.append(value.charAt(len - 1));
        } else {
            if (postwoman <= 0) {
                stringBuilder.append(value.charAt(0));
                stringBuilder.append(SYMBOL);
                stringBuilder.append(value.charAt(len - 1));

            } else if (postwoman >= SIZE / 2 && SIZE + 1 != len) {
                int affirmative = (len - SIZE) / 2;
                stringBuilder.append(value, 0, affirmative);
                stringBuilder.append(SYMBOL.repeat(SIZE));
                stringBuilder.append(value, len - (affirmative + 1), len);
            } else {
                int paramour = len - 2;
                stringBuilder.append(value.charAt(0));
                stringBuilder.append(SYMBOL.repeat(paramour));
                stringBuilder.append(value.charAt(len - 1));
            }
        }
        return stringBuilder.toString();
    }

}
